home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8095 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  72 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.netins.net!isac!gg
  3. From: gg@isac.hces.com (Greg Goodrich)
  4. Subject: Re: Processing a structure- how?
  5. Message-ID: <1996Feb29.232302.5640@isac.hces.com>
  6. Organization: Health Care Expert Systems
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <DnDv98.Ko1@network.com>
  9. Date: Thu, 29 Feb 1996 23:23:02 GMT
  10.  
  11. Mike Collins (collim@anubis.network.com) wrote:
  12. : Folks,
  13.  
  14. : I want to write a generic routine that will print data contained 
  15. : in structures on the screen (of a DOS-based PC, but I don't think that 
  16. : comes into it). What I want is to be able to pass a structure  or 
  17. : structure pointer to the routine, which will then be able to find out what 
  18. : the contents of the structure are, so that if I pass a customer structure 
  19. : containing surname, forename, address, phone number, I will be able to 
  20. : print this. If I pass a different structure containing catalog data, I 
  21. : will be able to print Item, category, bin location, etc.
  22.  
  23. You can make this happen, but it takes some coding.  What you need is a
  24. mechanism to determine what the structure contains.  You can set up
  25. something like this:
  26.  
  27. struct {
  28.     char *name;
  29.     short type;
  30.     short length;
  31.     void *data;
  32.     } DEFINITION;
  33.  
  34. /* a sample array to be defined using above definition struct */
  35. typedef struct {
  36.     char *mem1;
  37.     int mem2;
  38.     short mem3;
  39.     char mem4;
  40.     } arr1;
  41.  
  42. #define T_SHORT    1
  43. #define T_INT    2
  44. #define T_CHAR    3
  45. #define T_STR    4
  46.  
  47. DEFINITION darr1[] = {
  48.     { "mem1", T_STR, sizeof(char*), offsetof(arr1, mem1) },
  49.     { "mem2", T_INT, sizeof(char*), offsetof(arr1, mem1) },
  50.     { "mem3", T_SHORT, sizeof(char*), offsetof(arr1, mem1) },
  51.     { "mem4", T_CHAR, sizeof(char*), offsetof(arr1, mem1) },
  52.     { "", 0, 0, 0 }
  53.     };
  54.  
  55.  
  56. Now you can create an array of these structures, and initialize these
  57. structures with the proper data for each structure that you may pass in
  58. to a function.  You then need only to look at the above structure for
  59. each of your structure members to determine all you need about the
  60. member to manipulate it.  This can be very confusing, and therefore you
  61. should probably research greatly before attempting to code it.  BTW, you
  62. need to use the macro offsetof() to determine where your structure
  63. members are relative to the beginning of the structure for the "data"
  64. pointer in the above structure definition.  Good luck!
  65.  
  66. Greg.
  67. -- 
  68. _______________________________________
  69.     Greg Goodrich - gg@hces.com
  70.     Software Engineer
  71.     PACE Health Management Systems
  72.